package com.erp.gather.controller;

import com.erp.gather.domain.bo.ErpSellerStoreCommodityRankStatisticsBo;
import com.erp.gather.domain.vo.ErpSellerStoreCommodityRankStatisticsVo;
import com.erp.gather.service.IErpSellerStoreCommodityRankStatisticsService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.gather.api.domain.dto.EvaluationProductDto;
import com.ruoyi.gather.api.domain.dto.ProductInfoModuleDto;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺商品排行记录
 *
 * @author erp
 * @date 2024-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStoreCommodityRankStatistics")
public class ErpSellerStoreCommodityRankStatisticsController extends BaseController {

    private final IErpSellerStoreCommodityRankStatisticsService iErpSellerStoreCommodityRankStatisticsService;

    /**
     * 是否出单
     * @param productId aeProductId
     * @return true：出单
     */
    @GetMapping("/isOutOrder/{productId}")
    public Boolean isOutOrder(@PathVariable Long productId) {
        return iErpSellerStoreCommodityRankStatisticsService.isOutOrder(productId);
    }
 
    @GetMapping("/getProductRankByStoreId/{outOrder}/{productSource}/{storeId}/{limit}")
    List<ProductInfoModuleDto> getProductRankByStoreId(@PathVariable String outOrder, @PathVariable Integer productSource, @PathVariable Long storeId
            , @PathVariable Integer limit) {
        return iErpSellerStoreCommodityRankStatisticsService
                .getProductRankByStoreId(outOrder, productSource, storeId, limit);
    }

    @GetMapping("/getProductsRankByPrimary/{productId}/{productSource}/{productOut}")
    List<Long> getProductsRankByPrimary(@PathVariable("productId") Long productId,@PathVariable("productSource")
                    Integer productSource,@PathVariable("productOut") Integer productOut) {
        return iErpSellerStoreCommodityRankStatisticsService.getProductsRankByPrimary(productId, productSource, productOut);
    }
    @GetMapping("/getEvaluationProduct")
    public List<EvaluationProductDto> getEvaluationProduct(@RequestParam("storeId") Long storeId, 
                     @RequestParam(value = "shieldProductIds", required = false) String shieldProductIds,
                      @RequestParam("maxBig") String maxBig,@RequestParam("minBig") String minBig) {
        List<String> collect = new ArrayList<>();
        if (StringUtils.isNotEmpty(shieldProductIds)) {
            collect = Arrays.stream(shieldProductIds.split(",")).collect(Collectors.toList());
        }
        return iErpSellerStoreCommodityRankStatisticsService.getEvaluationProduct(storeId,collect,maxBig,minBig);
    }

    /**
     * 查询店铺商品排行记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpSellerStoreCommodityRankStatisticsVo> list(ErpSellerStoreCommodityRankStatisticsBo bo, PageQuery pageQuery) {
        return iErpSellerStoreCommodityRankStatisticsService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取店铺商品排行记录详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpSellerStoreCommodityRankStatisticsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpSellerStoreCommodityRankStatisticsService.queryById(id));
    }

    /**
     * 新增店铺商品排行记录
     */
    @Log(title = "店铺商品排行记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStoreCommodityRankStatisticsBo bo) {
        return toAjax(iErpSellerStoreCommodityRankStatisticsService.insertByBo(bo));
    }

    /**
     * 修改店铺商品排行记录
     */
    @Log(title = "店铺商品排行记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStoreCommodityRankStatisticsBo bo) {
        return toAjax(iErpSellerStoreCommodityRankStatisticsService.updateByBo(bo));
    }

    /**
     * 删除店铺商品排行记录
     *
     * @param ids 主键串
     */
    @Log(title = "店铺商品排行记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpSellerStoreCommodityRankStatisticsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 查询周期内的访客量
     * @param aeProductId
     * @param statisticalPeriod
     * @return
     */
    @GetMapping("/getVisitorsTotalsByTime/{aeProductId}/{statisticalPeriod}")
    public R<Integer> getVisitorsTotalsByTime(@PathVariable("aeProductId") String aeProductId,
                                              @PathVariable("statisticalPeriod") String statisticalPeriod){
        return R.ok(iErpSellerStoreCommodityRankStatisticsService.selectVisitorsTotalsByTime(Long.parseLong(aeProductId), Integer.parseInt(statisticalPeriod)));
    }
}
